package com.dxy.hms.auth;

import cn.hutool.core.collection.CollUtil;
import com.dxy.hms.base.AppConstant;
import com.dxy.hms.config.InitInfoProperties;
import com.dxy.hms.model.entity.Role;
import com.dxy.hms.model.entity.User;
import com.dxy.hms.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class DefaultLoginUser implements LoginUser {

    private static InitInfoProperties initInfoProperties;
    private static RoleMapper roleMapper;

    @Autowired
    public void setAdminInfoProperties(InitInfoProperties initInfoProperties) {
        DefaultLoginUser.initInfoProperties = initInfoProperties;
    }

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        DefaultLoginUser.roleMapper = roleMapper;
    }

    private User loginUser;

    public DefaultLoginUser() {
    }

    public DefaultLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }

    @Override
    public Long getUserId() {
        return loginUser.getId();
    }

    @Override
    public String getUserName() {
        return loginUser.getAccount();
    }

    @Override
    public Set<String> getRoleList() {
        List<Role> roleList = roleMapper.getRoleByUserId(getUserId());
        if (CollUtil.isNotEmpty(roleList)) {
            return roleList.stream().map(Role::getRoleCode).collect(Collectors.toSet());
        }
        return null;
    }

    /**
     * 是否是超级管理员（超管可以没有数据权限）
     * 此处使用角色来判断（可能会创建一些超管用户分给别人使用，但这些用户是可以回收的）
     * @return
     */
    @Override
    public boolean isSuperAdmin() {
        return AppConstant.SUPER_ADMIN_USER_TYPE.equals(loginUser.getType());
    }
}
